package com.itheima.io.字节流;

import java.io.*;

public class 文件复制 {
    public static void main(String[] args) throws IOException {
        File file = new File("vue.mp4");
       // File file1 = new File("G:\\1808-19谢康立-更改页面内容\\js\\jquery-3.4.1.js");
        File file1 = new File("F:\\开发\\在家学习\\其他文件\\在家学习\\课本\\SSM框架技术精讲与整合案例\\528999 Spring+Spring MVC+MyBatis框架技术精讲与整合案例-光盘\\上传光盘\\视频\\第18章\\18.3.1  Vue简介 18.3.2  第一个Vue应用.mp4");

       /* copyFile1(file,file1);
        copyFile2(file,file1);
        copyFile3(file, file1);*/
        copyFile4(file, file1);


    }

    private static void copyFile4(File file, File file1) throws IOException {
        long stat = System.currentTimeMillis();
        BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(file));
        BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file1));

        //定义中间量
        byte[] bytes = new byte[1024];
        int len;

        //循环读写
        while ((len = fis.read(bytes)) != -1){
            fos.write(bytes,0,len);
        }

        //释放资源
        fis.close();
        fos.close();

        long end = System.currentTimeMillis();
        System.out.println(end -stat);

        //file.delete();
    }


    //文件复制      -- 基于缓冲字节流实现一次读写一个字节
    private static void copyFile3(File file, File file1) throws IOException {
        long start = System.currentTimeMillis();
        //创建输入流,输出流对象
            //在底层创建了一个默认长度为8192的数组
        BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(file));
        BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file1));

        //定义中间量
        int bite;

        //循环读写
        while ((bite = fis.read()) != -1) {
            fos.write(bite);
        }

        //释放资源
        fos.close();
        fis.close();

        file.delete();
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }

    //文件复制      -- 基于普通字节流实现一次读取一个字节
    private static void copyFile1(File file,File file1) throws IOException {
        long start = System.currentTimeMillis();
        FileOutputStream fos = new FileOutputStream(file);
        FileInputStream fis = new FileInputStream(file1);

        int bite;
        while ((bite = fis.read()) != -1) {
            fos.write(bite);
        }

        fos.close();
        fis.close();
        long end = System.currentTimeMillis();
        System.out.println(end-start);

        System.out.println(file.delete());
    }

    //文件复制      -- 基于普通字节流实现一次读取一个字节数组
    private static void copyFile2(File file,File file1) throws IOException {
        long start = System.currentTimeMillis();
        FileOutputStream fos = new FileOutputStream(file);
        FileInputStream fis = new FileInputStream(file1);

        //定义临时变量,充当传输数据的载体
        byte[] bytes = new byte[1024];
        int len;
        //循环读写
        while ((len = fis.read(bytes)) != -1){
            fos.write(bytes,0,len);
        }

        //释放资源
        fos.close();
        fis.close();
        long end = System.currentTimeMillis();
        System.out.println(end-start);
        System.out.println(file.delete());
    }
}
